<template>
    <el-dialog
            :title="!dataForm.orderId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="报修单ID" prop="reportId">
                        <el-input v-model.number="dataForm.reportId" placeholder="报修单ID"></el-input>
                </el-form-item>
                <el-form-item label="报修渠道（移动端、电话、系统内等）" prop="reportChannel">
                        <el-input v-model="dataForm.reportChannel" placeholder="报修渠道（移动端、电话、系统内等）"></el-input>
                </el-form-item>
                <el-form-item label="故障设备ID" prop="deviceId">
                        <el-input v-model="dataForm.deviceId" placeholder="故障设备ID"></el-input>
                </el-form-item>
                <el-form-item label="报修时间" prop="reportTime">
                        <el-date-picker
                                v-model="dataForm.reportTime"
                                type="datetime"
                                placeholder="报修时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="报修人姓名" prop="reporterName">
                        <el-input v-model="dataForm.reporterName" placeholder="报修人姓名"></el-input>
                </el-form-item>
                <el-form-item label="报修人联系方式" prop="reporterContact">
                        <el-input v-model="dataForm.reporterContact" placeholder="报修人联系方式"></el-input>
                </el-form-item>
                <el-form-item label="报修状态" prop="status">
                        <el-input v-model="dataForm.status" placeholder="报修状态"></el-input>
                </el-form-item>
                <el-form-item label="记录创建时间" prop="createTime">
                        <el-date-picker
                                v-model="dataForm.createTime"
                                type="datetime"
                                placeholder="记录创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="记录更新时间" prop="updateTime">
                        <el-date-picker
                                v-model="dataForm.updateTime"
                                type="datetime"
                                placeholder="记录更新时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="故障现象描述" prop="faultDescription">
                        <el-input v-model="dataForm.faultDescription" placeholder="故障现象描述"></el-input>
                </el-form-item>
                <el-form-item label="故障初步判断" prop="preliminaryDiagnosis">
                        <el-input v-model="dataForm.preliminaryDiagnosis" placeholder="故障初步判断"></el-input>
                </el-form-item>
                <el-form-item label="维修方案或方向" prop="repairPlan">
                        <el-input v-model="dataForm.repairPlan" placeholder="维修方案或方向"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
        data () {
            return {
                visible: false,
                dataForm: {
                    reportId: null,
                    reportChannel: null,
                    deviceId: null,
                    reportTime: null,
                    reporterName: null,
                    reporterContact: null,
                    status: null,
                    createTime: null,
                    updateTime: null,
                    faultDescription: null,
                    preliminaryDiagnosis: null,
                    repairPlan: null,
                },
                dataRule: {
                    reportId: [
                        { required: true, message: '报修单ID不能为空', trigger: 'blur' }
                    ],
                    reportChannel: [
                        { required: true, message: '报修渠道（移动端、电话、系统内等）不能为空', trigger: 'blur' }
                    ],
                    deviceId: [
                        { required: true, message: '故障设备ID不能为空', trigger: 'blur' }
                    ],
                    reportTime: [
                        { required: true, message: '报修时间不能为空', trigger: 'blur' }
                    ],
                    reporterName: [
                        { required: true, message: '报修人姓名不能为空', trigger: 'blur' }
                    ],
                    reporterContact: [
                        { required: true, message: '报修人联系方式不能为空', trigger: 'blur' }
                    ],
                    status: [
                        { required: true, message: '报修状态不能为空', trigger: 'blur' }
                    ],
                    createTime: [
                        { required: true, message: '记录创建时间不能为空', trigger: 'blur' }
                    ],
                    updateTime: [
                        { required: true, message: '记录更新时间不能为空', trigger: 'blur' }
                    ],
                    faultDescription: [
                        { required: true, message: '故障现象描述不能为空', trigger: 'blur' }
                    ],
                    preliminaryDiagnosis: [
                        { required: true, message: '故障初步判断不能为空', trigger: 'blur' }
                    ],
                    repairPlan: [
                        { required: true, message: '维修方案或方向不能为空', trigger: 'blur' }
                    ],
                }
            }
        },
        methods: {
            init (id) {
                this.dataForm.orderId = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    this.dataForm.orderId = id || 0
                    if (this.dataForm.orderId) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppFaultReport/info/\\$\\{this.dataForm.orderId}`),
                            method: 'get',
                            params: this.$http.adornParams()
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.dataForm = data.appfaultreport
                            }
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit () {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppFaultReport/\\$\\{!this.dataForm.orderId ? 'save' : 'update'}`),
                            method: 'post',
                            data: this.$http.adornData(this.dataForm)
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        }
    }
</script>